
package com.kweative.model.column;

import com.kweative.model.Table;
import com.kweative.model.query.QueryUtil;
import com.kweative.model.sql.SQLDataType;
import com.kweative.util.ArrayUtil;

/**
 *
 * @author henrik
 */
public class ColumnEnum<T extends Table> extends Column<ColumnEnum,T> {
    
    public ColumnEnum(String name, Class<Enum> clazz) {
        this(clazz);
		setName(name);
    }

	public ColumnEnum(Class<Enum> clazz) {
		super(clazz);
		setSQLDataType(SQLDataType.ENUM);
		Object[] enumConstants = clazz.getEnumConstants();
		setSQLTypeParms(
				ArrayUtil.join(QueryUtil.formatValue(this,enumConstants),",")
		);
		setSQLDefault(enumConstants[0]);
	}


    @Override
    public String serialize(Object value) {
        return String.format("'%s'",((Enum)value).toString());
    }

    @Override
    public Enum unserialize(String value) {
        return Enum.valueOf(getDataType(),value);
    }

}
